package com.ruin.tech.ahri.service.impl;

import com.ruin.tech.ahri.dao.ArticleMapper;
import com.ruin.tech.ahri.dao.CommentMapper;
import com.ruin.tech.ahri.dao.UserMapper;
import com.ruin.tech.ahri.model.dto.*;
import com.ruin.tech.ahri.model.vo.CommentVO;
import com.ruin.tech.ahri.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program ahri
 * @description: 评论相关的服务
 * @author: ruin
 * @create: 2019/12/14 22:06
 */
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommentVO submitComment(Comment comment) throws Exception {
        if(comment.getUserId() == null){
            comment.setUserId(0L);
        }
        comment.setUpdateTime(new Date());
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);

        if(comment.getParentId() == 0){
            ArticleExample articleExample = new ArticleExample();
            articleExample.createCriteria().andIdEqualTo(comment.getArticleId());
            List<Article> articles = articleMapper.selectByExample(articleExample);
            Article article = articles.get(0);
            article.setCommentCount((articles.get(0).getCommentCount()==null?0:articles.get(0).getCommentCount())+1);
            articleMapper.updateByExampleSelective(article,articleExample);
        }

        CommentVO commentVO = new CommentVO();
        commentVO.setId(comment.getId());
        commentVO.setParentId(comment.getParentId());
        commentVO.setCreateTime(comment.getCreateTime());
        commentVO.setContent(comment.getContent());
        commentVO.setReplyList(new ArrayList<>());
        commentVO.setCommenterAvatar("");
        commentVO.setCommenter("匿名");
        if(comment.getUserId() != null && !(comment.getUserId()).equals(Long.valueOf(0L))){
            UserExample example = new UserExample();
            example.createCriteria().andIdEqualTo(comment.getUserId());
            List<User> users = userMapper.selectByExample(example);
            if(users != null && users.size()>0){
                commentVO.setCommenterAvatar(users.get(0).getHeaderPath());
                commentVO.setCommenter(users.get(0).getNickName());
            }
        }
        return commentVO;
    }

    @Override
    public List<CommentVO> findCommentsByArticleId(ArticleDTO param) {
        Long articleId = param.getId();
        Integer showCommentNum = param.getShowCommentNum();
        List<CommentVO> commentList = commentMapper.selectCommentsByArticleId(articleId,showCommentNum);
        List<Long> ids = new ArrayList<>();
        for(CommentVO commentVO : commentList){
            ids.add(commentVO.getId());
        }
        List<CommentVO> replyList = commentMapper.selectReplysByParentIds(ids);

        if(ids.size() > 0) {
            for (CommentVO commentVO : commentList) {
                for (CommentVO reply : replyList) {
                    if (reply.getParentId() != null && reply.getParentId().equals(commentVO.getId())) {
                        commentVO.getReplyList().add(reply);
                    }
                }
            }
        }
        return commentList;
    }

    @Override
    public List<CommentVO> findCommentsByUserId(ArticleDTO param) {
        Long userId = param.getLoginUserId();
        Integer showCommentNum = param.getShowCommentNum();
        List<CommentVO> commentList = commentMapper.selectCommentsByUserId(userId,showCommentNum);
        List<Long> ids = new ArrayList<>();
        for(CommentVO commentVO : commentList){
            ids.add(commentVO.getId());
        }

        if(ids.size() > 0){
            List<CommentVO> replyList = commentMapper.selectReplysByParentIds(ids);

            for(CommentVO commentVO : commentList){
                for(CommentVO reply: replyList){
                    if(reply.getParentId() != null && reply.getParentId().equals(commentVO.getId())){
                        commentVO.getReplyList().add(reply);
                    }
                }
            }
        }

        return commentList;
    }


}
